package br.com.kliptus.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.kliptus.application.ApplicationConstants;
import br.com.kliptus.model.bean.UserJB;

@WebFilter("/pages/*")
public class SecurityFiter implements Filter {

    public SecurityFiter() {
        
    }

	public void destroy() {

	}
	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest  httpServletRequest = (HttpServletRequest)request;
		HttpServletResponse  httpServletResponse = (HttpServletResponse)response;
		
		UserJB user = (UserJB) httpServletRequest.getSession().getAttribute(ApplicationConstants.USER_SESSION);
		
		if (user != null)
			chain.doFilter(request, response);
		else
			httpServletResponse.sendRedirect(ApplicationConstants.PAGE_LOGIN);
		
	}

	public void init(FilterConfig fConfig) throws ServletException {

	}

}
